﻿using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace ClassLibrary {
    //http://snipplr.com/view/36749/
    public partial class ProgressDialog : Form {
        private bool isLongOperation;

        public ProgressDialog(bool isLogoVisible) {
            InitializeComponent();
            isLongOperation = false;
            pbxLogo.Visible = isLogoVisible;
        }

        public ProgressDialog(bool isLogoVisible, DoWorkEventHandler work) {
            InitializeComponent();
            isLongOperation = true;
            backgroundWorker.DoWork += work; // Event handler to be called in context of new thread.
            backgroundWorker.ProgressChanged+=new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
            backgroundWorker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
            pbxLogo.Visible = isLogoVisible;
        }

        private void UtfprFormSplash_Load(object sender, EventArgs e) {
            backgroundWorker.RunWorkerAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
            if (isLongOperation) {
                lblStatus.Text = "wait...";
                backgroundWorker.CancelAsync(); // Tell worker to abort.
                btnCancel.Enabled = false;
            } else {
                Close();
            }
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
            pgbStatus.Value = e.ProgressPercentage;
            //lblStatus.Text = e.UserState as string;
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
